package com.example.mall.auth.svc.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.example.mall.auth.svc.service.AuthAccountService;
import com.example.mall.common.model.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author Mickey
 * @since 2022/4/9
 **/
@AllArgsConstructor
@RestController
@RequestMapping("/auth")
public class AuthController {

    private final AuthAccountService authAccountService;

    @PostMapping("/login")
    public Result<?> login(@RequestParam() String uniqueId) {
        return Result.OK(authAccountService.getOrCreate(uniqueId));
    }

    @GetMapping("/doLogin")
    public Object doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if ("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            return StpUtil.getTokenInfo();
        }
        return "登录失败";
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @GetMapping("/isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

}
